﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections.Generic;
using MySql.Data.MySqlClient;


public class GerenteDAO
{
    MySqlConnection connection = Database.getInstance.getConnection();

    public GerenteDAO()
    {

    }

    public List<GerenteDTO> getAll()
    {
        try
        {
            MySqlCommand command = connection.CreateCommand();
            MySqlDataReader reader;

            command.CommandText = "SELECT * FROM gerente";
            connection.Open();

            List<GerenteDTO> Gerente_List = new List<GerenteDTO>();

            reader = command.ExecuteReader();
            while (reader.Read())
            {
                GerenteDTO gerente = new GerenteDTO();
                gerente.Idgerente = Convert.ToInt32(reader["idgerente"]);
                gerente.Cpf = (string)reader["cpf"];
                gerente.Telefone = (string)reader["telefone"];
                Gerente_List.Add(gerente);
            }
            connection.Close();
            return Gerente_List;
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }

    public bool insere(GerenteDTO gerente)
    {
        try
        {
            MySqlCommand command = connection.CreateCommand();

            command.CommandText = "INSERT INTO gerente (cpf, telefone, usuario_idusuario) VALUES (@cpf, @telefone, @usuario_idusuario)";
            connection.Open();

            command.Parameters.AddWithValue("@cpf", gerente.Cpf);
            command.Parameters.AddWithValue("@telefone", gerente.Telefone);
            command.Parameters.AddWithValue("@usuario_idusuario", gerente.Usuario_idusuario);
            command.ExecuteNonQuery();
            connection.Close();
            return true;
        }
        catch (SqlException ex)
        {
            return false;
        }
    }

    public bool deleta(int idgerente)
    {
        try
        {
            MySqlCommand command = connection.CreateCommand();

            command.CommandText = "DELETE FROM gerente WHERE idgerente=@idgerente";
            connection.Open();

            command.Parameters.AddWithValue("@idgerente", idgerente);

            command.ExecuteNonQuery();
            connection.Close();
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

    public bool atualiza(GerenteDTO gerente)
    {

        try
        {

            MySqlCommand command = connection.CreateCommand();

            command.CommandText = "UPDATE usuario SET cpf=@cpf, telefone=@telefone WHERE idgerente=@idgerente";
            connection.Open();

            command.Parameters.AddWithValue("@cpf", gerente.Cpf);
            command.Parameters.AddWithValue("@telefone", gerente.Telefone);
            command.Parameters.AddWithValue("@idgerente", gerente.Idgerente);

            command.ExecuteNonQuery();
            connection.Close();
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }

    }
}
